/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.fzhucc.modules.sciStudio.service;

import com.fzhucc.modules.sciStudio.domain.SciStudioImg;
import com.fzhucc.utils.ValidationUtil;
import com.fzhucc.utils.FileUtil;
import com.fzhucc.utils.PageUtil;
import com.fzhucc.utils.QueryHelp;
import lombok.RequiredArgsConstructor;
import com.fzhucc.modules.sciStudio.repository.SciStudioImgRepository;
import com.fzhucc.modules.sciStudio.domain.dto.SciStudioImgDto;
import com.fzhucc.modules.sciStudio.domain.dto.SciStudioImgQueryCriteria;
import com.fzhucc.modules.sciStudio.service.mapstruct.SciStudioImgMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author yyg
* @date 2024-01-10
**/
@Service
@RequiredArgsConstructor
public class SciStudioImgService {

    private final SciStudioImgRepository sciStudioImgRepository;
    private final SciStudioImgMapper sciStudioImgMapper;

    public Map<String,Object> queryAll(SciStudioImgQueryCriteria criteria, Pageable pageable){
        Page<SciStudioImg> page = sciStudioImgRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(sciStudioImgMapper::toDto));
    }

    public List<SciStudioImgDto> queryAll(SciStudioImgQueryCriteria criteria){
        return sciStudioImgMapper.toDto(sciStudioImgRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Transactional
    public SciStudioImgDto findById(Integer id) {
        SciStudioImg sciStudioImg = sciStudioImgRepository.findById(id).orElseGet(SciStudioImg::new);
        ValidationUtil.isNull(sciStudioImg.getId(),"SciStudioImg","id",id);
        return sciStudioImgMapper.toDto(sciStudioImg);
    }

    @Transactional(rollbackFor = Exception.class)
    public SciStudioImgDto create(SciStudioImg resources) {
        return sciStudioImgMapper.toDto(sciStudioImgRepository.save(resources));
    }

    @Transactional(rollbackFor = Exception.class)
    public void update(SciStudioImg resources) {
        SciStudioImg sciStudioImg = sciStudioImgRepository.findById(resources.getId()).orElseGet(SciStudioImg::new);
        ValidationUtil.isNull( sciStudioImg.getId(),"SciStudioImg","id",resources.getId());
        sciStudioImg.copy(resources);
        sciStudioImgRepository.save(sciStudioImg);
    }

    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            sciStudioImgRepository.deleteById(id);
        }
    }

    public void download(List<SciStudioImgDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (SciStudioImgDto sciStudioImg : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("tb_sci_studio id", sciStudioImg.getStId());
            map.put("图片", sciStudioImg.getPic());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}